package bus_srv

import (
	"encoding/json"
	"github.com/patrickmn/go-cache"
	"gluenet/pkg/apis"
	v2 "gluenet/utils/rpc/v2/define"
)

func InstanceReport(ctx *apis.GlueContext) {
	ctx.Logger.Debugf("recive report")

	req := &v2.Report{}
	if err := json.Unmarshal(ctx.RpcContext.Data.Data, req); err != nil {
		ctx.Logger.Errorf("unmarshall report request failed")
		return
	}

	var data interface{}
	if err := json.Unmarshal([]byte(req.Data.(string)), &data); err != nil {
		ctx.Logger.Errorf("unmarshall data failed")
		return
	}

	ctx.Logger.Debugf("revcie report guid %v", req.Guid)

	if value, ok := ctx.Report.Get(req.Guid); ok {
		value.(map[string]interface{})[req.Op] = data
	} else {
		ctx.Report.Set(req.Guid, map[string]interface{}{
			req.Op: data,
		}, cache.NoExpiration)
	}
}
